define('userinfo/index',function(){
	var ViewModel = function (){
		var self=this;
		self.email = ko.observable('').extend({required:{message:'请输入您的邮箱地址'}}).extend({pattern:{message:'请输入正确的邮箱地址',params:'([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$'}});
		toastr.options = {
				"closeButton": false, //是否显示关闭按钮
				"debug": false, //是否使用debug模式
				"positionClass": "toast-top-full-width",//弹出窗的位置
				"showDuration": "300",//显示的动画时间
				"hideDuration": "1000",//消失的动画时间
				"timeOut": "3000", //展现时间
				"extendedTimeOut": "1000",//加长展示时间
				"showEasing": "swing",//显示时的动画缓冲方式
				"hideEasing": "linear",//消失时的动画缓冲方式
				"showMethod": "fadeIn",//显示时的动画方式
				"hideMethod": "fadeOut" //消失时的动画方式
		};
		self.userInfoModel=ko.observable({
			uname:'',
			pwdShow:ko.observable(true),
			password:'',
			mobile:'',
			email:'',
			departmentname:'',
			departmentid:'',
			postid:''
		});
		self.uname = ko.observable()
	
		var url ="/userinfo/doFindObject";
		var params={
				"pageSize":'',
				"pageIndex":''
		};
		//第一个参数请求路径，第二个是传入的参数，第三个是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax(url, params, self.userInfo);
		self.userInfo = function(result){
			var data = result.data;
			if(data && data.length && data.length>0){
				$.each(data, function (index, val) { val.checked = false; });
			}
			var items = ko.mapping.fromJS(data);
			return items();
		}
		/**添加*/
		var layertitle = "";
		self.show_add = function(){
			self.userInfoModel({
				uname:'',
				mobile:'',
				email:'',
				departmentname:'',
				departmentid:''
			})
			$("#txt_dep").focus(function(){
				$("#treeContent").show();
				self.showZtree();
				self.ztreeblur();
			});
			layertitle = "添加用户";
			self.layer();
		}
		/**修改*/
		self.update = function(data){
			layertitle = "修改用户";
			self.userInfoModel().departmentname = $("#txt_dep").val();
			self.userInfoModel().departmentid = $("#txtdepid").val();
			self.userInfoModel(data);
			$("#txt_dep").focus(function(){
				$("#treeContent").show();
				self.showZtree();
				self.ztreeblur();
			});
			self.layer();
			self.findPost();
		}
		self.userinfo = ko.observable({
			departmentname:'',
			uname:''
		
		})
		self.ztreeblur= function(){
			$("#txt_dep").blur(function(){
				setTimeout(function(){
						$("#treeContent").css("display","none")
				},200)
			});
			
		}
		/**查询*/
		self.search = function(){
			var data = self.userinfo();
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);
		}
		/**重置*/
		self.reset = function(){
			self.userinfo({
				departmentname:'',
				uname:''
			})
		}
		/*self.change = function(){

		}*/
		//隐藏展开搜索框
		self.showText = ko.observable('收缩');
		self.showorno = ko.observable(true);
		self.noshow= function(){
			if(self.showText() =='收缩' ){
				self.showText('展开');
				self.showorno(false);
			}else{
				self.showText('收缩');
				self.showorno(true);
			}
		}
		self.unamedo=function(data){
			$("#uname").blur(function(){
				console.log(data.uname);
				if(data.uname == ""){
					return;
				}else{
				var url = "/passport/doFindName";
				$.ajax({
					url:url,
					data:data.uname,
					type:"post",
					dataType:"json",
					contentType:"application/json;charset=UTF-8",
					success:function(data){
						console.log(data);
						if(data.result == 0){
							$("#unamedo").html("");
						}
						if(data.result ==1){
							$("#unamedo").html("用户名已被占用!");
						}
					}
				});
				}
			})
		}
	
		/**弹窗封装*/
		self.layer = function(){
			layer.open({
				type : 1,
				title:layertitle,
				skin : 'layui-layer-rim', //加上边框
				area : [ '400px', '400px' ], //宽高
				content :$('#popUser')
			});
		};
		self.CurrentIndex = ko.observable(0);
		self.layerdepclick = function(){
			var  currentIndex = self.CurrentIndex();
			currentIndex=currentIndex+1;
			self.CurrentIndex(currentIndex);
			console.log(currentIndex);
			if(currentIndex%2 ==0){
				$("#treeContent").css("display","none");
			}else{
				$("#treeContent").show();
				$("#txt_dep").focus(function(){
					self.showZtree();
					self.ztreeblur();
				});
			}

		}
		self.showZtree = function(){
			var tree ={
					pFunction: {  
						zTree: '',  
						setting:{ 
							view: {
								dblClickExpand: false,
								selectedMulti: false,
								showTitle: false,
								showIcon: true
							},
							check:{  
								enable:false  
							},    
							data:    {  
								key: {
									name: "departmentName"
								},
								simpleData: {
									enable: true,
									idKey: "id",
									pIdKey: "pId",
									rootPId: null
								}  
							},   
							callback: {
								onClick: self.zTreeOnClick
							}
						}
					}
			};
			$.ajax({
				url:"/department/findZtreeObject",
				type:"post",
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				success:function(data){
					var zNodes =  data;
					console.log(zNodes);
					ztree = $.fn.zTree.init($("#treeDemo"), tree.pFunction.setting,zNodes);
					ztree.expandAll(true);
					console.log( ztree);
				}
			});
		}
		/**ztree并获取名字*/
		var node;
		self.zTreeOnClick  = function(event,treeId,treeNode){
			node = ztree.getSelectedNodes();
			$("#txt_dep").val(node[0].departmentName);
			$("#txtdepid").val(node[0].id);
			self.findPost();
		};
		self.findPost= function(){
			self.userInfoModel().deparmentid = $("#txtdepid").val();
			var departmentId=self.userInfoModel().deparmentid;
			$.ajax({
				url:"/post/doFindPost",
				data:JSON.stringify(departmentId),
				type:"post",
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				success:function(data){
					console.log(data);
					self.availablePosts(data);
				}
			});
		}
		//下拉选
		self.postSelected=ko.observable();
		self.availablePosts=ko.observable([]);

		/**
		 * 添加和修改
		 */
		self.submit = function(data){
			if(data!=null){
				self.userInfoModel().departmentid=$("#txtdepid").val();
				self.userInfoModel().postid = self.postSelected();
			}
			console.log(self.userInfoModel().departmentname);
			var saveObjectUrl = "/userinfo/doSaveObject";
			var updataOnjectUrl = "/userinfo/doUpdataObject";
			var url = data.uid?updataOnjectUrl:saveObjectUrl;
			$.ajax({
				url:url,
				data:JSON.stringify(data),
				type:"post",
				dataType:"json",
				contentType:"application/json;charset=UTF-8",
				success:function(data){
					if(data.result == 1){
						toastr.success("保存成功!!默认密码为:'123456'");
					}
					
					self.search();
				}
			});
			self.layerClose();
		}
		/**
		 * 关闭leyer弹窗
		 */
		self.layerClose = function(){
			layer.close(layer.index);
		}
		/*重置密码*/
		self.resetpw = function(data){
			layer.confirm('确定重置密码吗？重置后密码为:123456', {
				btn: ['确定','取消'] //按钮
			}, function(){
				var url = "/userinfo/doUpdataPw";
				$.ajax({
					url:url,
					data:JSON.stringify(data),
					type:"post",
					dataType:"json",
					contentType:"application/json;charset=UTF-8",
					success:function(data){
						if(data.result == 1){
							toastr.error("重置密码成功");
							self.layerClose();
						}
					}
				});
			})
		}
		self.deleteId = function(data){
			console.log(data.uid);
			layer.confirm('确定删除吗？', {
				btn: ['确定','取消'] //按钮
			}, function(){   //点击确定访问后台
				var url = "/userinfo/doDeleteObject";
				$.ajax({
					url:url,
					data:JSON.stringify(data.uid),
					type:"post",
					dataType:"json",
					contentType:"application/json;charset=UTF-8",
					success:function(data){
						if(data.result == 1){
							//layer.msg('已删除', {icon: 1});
							toastr.error("删除成功");
							self.layerClose();
							self.search();
						}
					}
				});
			},
			function(){   
				toastr.success("已取消");
			});
		}
	}

	var model = new ViewModel();
	ko.applyBindings(model);
})